//
//  ZyaClientAddressCell.swift
//  SlaughterClient
//
//  Created by zwb on 2018/5/31.
//  Copyright © 2018年 zwb. All rights reserved.
//

import UIKit

class ZyaClientAddressCell: UITableViewCell {
    
    var updateAddress: (() -> Void)?
    var deleteAddress: (() -> Void)?
    var selectAddress: (() -> Void)?

    @IBOutlet weak var address: UILabel!
    @IBOutlet weak var isSelect: UIButton!
    
    var model: ZyaClientAddress? {
        didSet {
            
            if model?.kDel == 0 {
                let add = model!.kLinkName.unwrap + "     " + model!.kLinkPhone.unwrap + "\n" + model!.clientAdd.unwrap
                address.text = add + "（系统地址）"
            } else {
                address.text = model!.kLinkName.unwrap + "     " + model!.kLinkPhone.unwrap + "\n" + model!.clientAdd.unwrap
            }
            
            if model?.currMark == 1 {
                isSelect.setTitle("默认地址", for: UIControl.State())
                isSelect.setTitleColor(Theme.orange, for: UIControl.State())
                isSelect.setImage(#imageLiteral(resourceName: "s_select"), for: UIControl.State())
            } else {
                isSelect.setTitle("设为默认", for: UIControl.State())
                isSelect.setTitleColor(UIColor.black, for: UIControl.State())
                isSelect.setImage(#imageLiteral(resourceName: "s_unselect"), for: UIControl.State())
            }
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        isSelect.titleEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 0)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    @IBAction func updateAction(_ sender: UIButton) {
        if let action = updateAddress {
            action()
        }
    }
    
    @IBAction func deleteAction(_ sender: UIButton) {
        if let action = deleteAddress {
            action()
        }
    }
    
    @IBAction func selectAction(_ sender: UIButton) {
        guard let mark = model?.currMark, mark != 1 else {
            return
        }
        
        if let action = selectAddress {
            action()
        }
    }
}
